//
//  AccountDetailViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddAccountDetailViewController.h"
#import "JddCommonFunction.h"
#import "JddAccountDetailItemCell.h"
#import "SBJson.h"
#import "JddDataParser.h"

@interface JddAccountDetailViewController ()

@end

@implementation JddAccountDetailViewController

@synthesize dateLabel;
@synthesize ListTable;
@synthesize tipLable;
@synthesize typeLable;
@synthesize cellvalue;
@synthesize prev;
@synthesize next;
@synthesize selecttype;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddAccountDetailViewController);

-(void)destoryView{
    if ([cellvalue count]>0) {
        [cellvalue removeAllObjects];
    }
    [ListTable reloadData];
}

-(IBAction)selectNext:(id)sender{
    if(hasNext){
        pageNo ++;
        [self startQueryOperation];
    }
}

-(IBAction)selectPrev:(id)sender{
    if (hasPrev) {
        pageNo --;
        [self startQueryOperation];
    }
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            dateLabel.text = @"当天";
            break;
        case 1:
            dateLabel.text = @"最近一周";
            break;
        case 2:
            dateLabel.text = @"最近一月";
            break;
        case 3:
            dateLabel.text = @"最近三月";
            break;
        default:
            break;
    }
    NSLog(@"%d", buttonIndex);
    if (buttonIndex != 4) {
        dateScope = buttonIndex;
    }
    pageNo = 1;
    [self startQueryOperation];
}

-(IBAction)selectType:(UISegmentedControl *)Seg{
    NSInteger selectValue = Seg.selectedSegmentIndex;
    switch (selectValue) {
        case 0:
            type = 0;            
            break;
        case 1:
            type = 1;
            break;
        case 2:		
            type = 2;
            break;
        case 3:
            type = 3;
            break;
        case 4:
            type = 4;
        default:
            break;
    }
    pageNo = 1;
    [self startQueryOperation];
}

- (IBAction)selectDateButtonClicked:(id)sender{
    UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:APP_NAME 
                                                    delegate:self 
                                           cancelButtonTitle:CANCEL
                                      destructiveButtonTitle:nil
                                           otherButtonTitles:@"当天", @"最近一周", @"最近一月", @"最近三月", nil];
    [as showInView:self.view];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cellvalue count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([cellvalue count]>0) {
        JddAccountDetailItemCell* cell = [JddAccountDetailItemCell getInstanceView];
        NSDictionary *celldata = [cellvalue objectAtIndex:indexPath.row];
        NSDateFormatter *outputFormat = [[NSDateFormatter alloc] init];
        [outputFormat setDateFormat:@"MM-dd"];
        
        NSString *datetemp = [celldata objectForKey:@"Time"];
//        NSString *text = [datetemp substringFromIndex:5];
        cell.dateLabel.text = datetemp;
        cell.typeLabel.text = [celldata objectForKey:@"Name"];
        cell.amountLabel.text = [celldata objectForKey:@"Money"];
        return cell;
    }else {
        UITableViewCell *cell = nil;
        cell.textLabel.text = @"相关数据不存在";
        return cell;
    }
}

- (void)startQueryOperation{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryAccountDetailOperation *operation = [[JddQueryAccountDetailOperation alloc] init];
    operation.delegate = self;
    operation.timeRegion = dateScope;
    operation.type = type;
    operation.pageNo = pageNo;
    operation.pageSize = pageSize;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
  //  [self.navigationItem setTitle:PAGE_ACCOUNT_DETAIL];
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_ACCOUNT_DETAIL];
        customLab.textAlignment = NSTextAlignmentCenter;
        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    ListTable.tableFooterView = v;
    [ListTable setSeparatorColor:SeparatorLineColor];
     
    if (IOS7) {
        [ListTable setSeparatorInset:UIEdgeInsetsZero];
    }
}


- (void)QueryAccountDetailOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        if ([JddDataParser parseModifyPasswordResult:result]) {
            SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
            NSDictionary *dictResult = [jsonParser objectWithData:result];
            NSMutableDictionary *data = [jsonParser objectWithString:[dictResult objectForKey:@"Data"]];
            NSMutableArray *resultArray = [data objectForKey:@"item"];
            
            if (pageNo > 1) {
                for (NSMutableArray *result in resultArray) {
                    [cellvalue addObject:result];
                }
            }else {
                cellvalue = resultArray;
                if ([cellvalue count]>0) {
                    tipLable.hidden = YES;
                }else {
                    tipLable.hidden = NO;
                    switch (dateScope) {
                        [self.ListTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
                        case 0:
                            tipLable.text = @"您当天没有相关记录";
                            break;
                        case 1:
                            tipLable.text = @"您最近一周没有相关记录";
                            break;
                        case 2:
                            tipLable.text = @"您最近一月没有相关记录";
                            break;
                        case 3:
                            tipLable.text = @"您最近三月没有相关记录";
                            break;
                        default:
                            break;
                    }
                    
                }
            }        
            if ([[data objectForKey:@"Count"] intValue] == [cellvalue count]) {
                next.hidden = YES;
                hasNext = NO;
            }else {
                next.hidden = NO;
                hasNext = YES;
            }
        if (type==0) {
            self.typeLable.text = @"类型";
        } else if (type == 4 || type == 1 || type == 2) {
            self.typeLable.text = @"状态";
        } else {
            self.typeLable.text = @"";
        }

        [ListTable reloadData];
        } else {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
    }
    
}
    
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"GouCaiJiLu"];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];   
    [MobClick beginLogPageView:@"GouCaiJiLu"];
    [MobClick event:@"UserCenter-zhanghumingxi"];

    next.hidden = YES;
    prev.hidden = YES;
    dateScope = 1;    
    pageNo = 1;
    type = 0;
    pageSize = 10;
    [selecttype setSelectedSegmentIndex:0];
    cellvalue = [[NSMutableArray alloc] init];
    [ListTable reloadData];
    dateLabel.text = @"最近一周";
    
    [self startQueryOperation];

}

- (void)viewDidUnload
{		
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
